package services

import (
	"feng-che.com/server/driver-quiz/internal/sysLogger"
	"github.com/sirupsen/logrus"
	"go.mongodb.org/mongo-driver/bson"
	"mongoDB"
	"time"
)

var cacheServiceList ServicesList

func ReadPublicServiceInCache() ServicesList {
	return cacheServiceList
}

func CacheAllAvailableService() {
	ctx, cancel := mongoDB.MongoDBTimeoutContext()
	defer cancel()
	result := ServicesList{}
	err := result.FindMany(ctx, &bson.D{
		*mongoDB.CommonFilterNotDeleted(),
		bson.E{Key: "stopService", Value: false},
	})
	if err != nil {
		sysLogger.SysLog.WithFields(logrus.Fields{
			"err": err.Error(),
		}).Warn("缓存服务失败")
		time.Sleep(1 * time.Minute)
		CacheAllAvailableService()
		return
	}
	cacheServiceList = result
	time.Sleep(1 * time.Minute)
	CacheAllAvailableService()
}
